package com.booter.merchant.controller;

import com.booter.merchant.service.MerchantOrderService;
import com.booter.model.Dict;
import com.booter.model.Merchant;
import com.booter.model.MerchantOrder;
import com.booter.model.User;
import org.apache.shardingsphere.core.strategy.keygen.SnowflakeShardingKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
public class MerchantController {

	/**
	 * 雪花算法生成的主键的二进制表示形式包含4部分，从高位到低位分别为：1bit符号位、41bit时间戳位、10bit工作进程位以及12bit序列号位
	 * 如果时钟回拨的时间超过最大容忍的毫秒数阈值，则程序报错；如果在可容忍的范围内，默认分布式主键生成器会等待时钟同步到最后一次主键生成的时间后再继续工作。
	 * 最大容忍的时钟回拨毫秒数的默认值为0，可通过调用静态方法DefaultKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds()设置
	 */
	private SnowflakeShardingKeyGenerator keyGenerator = new SnowflakeShardingKeyGenerator();

	@Autowired
	private MerchantOrderService merchantOrderService;

	@RequestMapping("merchant/order/create")
	public Long create() {
		MerchantOrder mo = new MerchantOrder();
		mo.setMid((Long)keyGenerator.generateKey());
		mo.setOrderId((Long)keyGenerator.generateKey());
		mo.setCreateTime(new Date());
		this.merchantOrderService.insert(mo);
		return mo.getId();
	}


	@RequestMapping("merchant/order/customer/create")
	public Long create(@RequestBody  MerchantOrder mo) {
//		MerchantOrder mo = new MerchantOrder();
//		mo.setMid((Long)keyGenerator.generateKey());
//		mo.setOrderId((Long)keyGenerator.generateKey());
		mo.setCreateTime(new Date());
		this.merchantOrderService.insert(mo);
		return mo.getId();
	}


	@RequestMapping("merchant/dict/create")
	public String createDict(@RequestParam Long id) {
		Dict dict = new Dict();
		dict.setId(id);
		dict.setCode("code");
		dict.setType("code");
		dict.setValue("1");
		this.merchantOrderService.insertDict(dict);
		return "ok";
	}


	@RequestMapping("merchant/user/create")
	public Long createUser(@RequestParam Long id) {
		User dict = new User();
		dict.setNickName("sxxx");
		dict.setPassword("password" + id);
		dict.setUserName("userName" + id);
		dict.setCreateTime(new Date());
		this.merchantOrderService.insertUser(dict);
		return dict.getId();
	}


	@RequestMapping("merchant/create")
	public Long creteUser() {
		Merchant m = new Merchant();
		m.setName("random");
		m.setCreateTime(new Date());
		this.merchantOrderService.insert(m);
		return m.getId();
	}

	@RequestMapping("merchant/order/{mid}")
	public List<MerchantOrder> getById(@PathVariable Long mid) {
		return this.merchantOrderService.getByMid(mid);
	}


	@RequestMapping("merchant/order/delete")
	public String delete(@RequestParam Long mid, @RequestParam Long id) {
		this.merchantOrderService.delete(id, mid);
		return "ok";
	}

	@RequestMapping("merchant/order/list")
	public List<MerchantOrder> list() {
		return this.merchantOrderService.selectMList(new HashMap<>());
	}


}
